package example.dao;

import example.entity.Book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图书数据访问对象
 *
 * @author 张心怡
 * @date 2024/12/16
 */
public class BookDao extends BaseDAO {

    // 添加图书方法
    public static void addBook(Book book) {
        String sql = "INSERT INTO book (name, author, score, count, ISBN, category) VALUES (?,?,?,?,?,?)";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, book.getName());
            preparedStatement.setString(2, book.getAuthor());
            preparedStatement.setFloat(3, book.getScore());
            preparedStatement.setInt(4, book.getCount());
            preparedStatement.setString(5, book.getISBN());
            preparedStatement.setString(6, book.getCategory());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据图书ID删除图书方法
    public static void deleteBook(int id) {
        String sql = "DELETE FROM book WHERE id =?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 更新图书信息方法
    public static void updateBook(Book book) {
        String sql = "UPDATE book SET name =?, author =?, score =?, count =?, ISBN =?, category =? WHERE id =?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, book.getName());
            preparedStatement.setString(2, book.getAuthor());
            preparedStatement.setFloat(3, book.getScore());
            preparedStatement.setInt(4, book.getCount());
            preparedStatement.setString(5, book.getISBN());
            preparedStatement.setString(6, book.getCategory());
            preparedStatement.setInt(7, book.getId());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据图书ID查询图书方法
    public static Book getBookById(int id) {
        String sql = "SELECT * FROM book WHERE id =?";
        Book book = null;
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, id);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    book = new Book();
                    book.setId(resultSet.getInt("id"));
                    book.setName(resultSet.getString("name"));
                    book.setAuthor(resultSet.getString("author"));
                    book.setScore(resultSet.getFloat("score"));
                    book.setCount(resultSet.getInt("count"));
                    book.setISBN(resultSet.getString("ISBN"));
                    book.setCategory(resultSet.getString("category"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    // 查询所有图书方法
    public static List<Book> getAllBooks() {
        List<Book> books = new ArrayList<>();
        String sql = "SELECT * FROM book";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setScore(resultSet.getFloat("score"));
                book.setCount(resultSet.getInt("count"));
                book.setISBN(resultSet.getString("ISBN"));
                book.setCategory(resultSet.getString("category"));
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }

    // 根据条件搜索图书方法（简单模糊搜索示例，可按需扩展）
    public static List<Book> searchBooks(String keyword) {
        List<Book> books = new ArrayList<>();
        String sql = "SELECT * FROM book WHERE name LIKE? OR author LIKE? OR ISBN LIKE?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, "%" + keyword + "%");
            preparedStatement.setString(2, "%" + keyword + "%");
            preparedStatement.setString(3, "%" + keyword + "%");
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    Book book = new Book();
                    book.setId(resultSet.getInt("id"));
                    book.setName(resultSet.getString("name"));
                    book.setAuthor(resultSet.getString("author"));
                    book.setScore(resultSet.getFloat("score"));
                    book.setCount(resultSet.getInt("count"));
                    book.setISBN(resultSet.getString("ISBN"));
                    book.setCategory(resultSet.getString("category"));
                    books.add(book);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }

    // 获取图书总数的方法（参考你提供的代码风格示例）
    public static int getTotalBooks() {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM book")) {
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}